<template>
	<view class="container">
		<view class="info">
			<!-- 手机号输入框 -->
			<view class="tel">
				<view class="input-group">
					<image class="icon" src="/static/my-update/手机号.png" mode="aspectFit"></image>
					<input class="tel-input" v-model="phoneNumber" placeholder="请输入新的手机号码" type="number" maxlength="11"
						@input="onPhoneInput" />
					<view class="clear-btn" v-if="phoneNumber" @click="clearPhone">
						<image class="clear-icon" src="/static/my-update/叉号.png" mode="aspectFit"></image>
					</view>
				</view>
			</view>

			<!-- 验证码输入和获取按钮 -->
			<view class="Yanzeng">
				<view class="YanzengMa">
					<image class="icon" src="/static/my-update/验证码.png" mode="aspectFit"></image>
					<input class="code-input" v-model="verificationCode" placeholder="输入验证码" type="number"
						maxlength="6" />
				</view>
				<view class="GetMa" @click="getVerificationCode">
					<text class="get-code-text">{{ getCodeButtonText }}</text>
				</view>
			</view>

			<!-- 提交按钮 -->
			<view class="submit-btn" @click="submitForm">
				<text class="submit-text">提交</text>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				phoneNumber: '', // 手机号码
				verificationCode: '', // 验证码
				countdown: 0, // 倒计时秒数
				timer: null, // 定时器
			}
		},
		computed: {
			// 获取验证码按钮文字
			getCodeButtonText() {
				return this.countdown > 0 ? `${this.countdown}秒` : '获取验证码';
			}
		},
		methods: {
			// 手机号输入事件
			onPhoneInput() {
				// 限制只能输入数字
				this.phoneNumber = this.phoneNumber.replace(/[^\d]/g, '');
			},

			// 清除手机号
			clearPhone() {
				this.phoneNumber = '';
			},

			// 获取验证码
			getVerificationCode() {
				// 如果正在倒计时，不允许再次点击
				if (this.countdown > 0) {
					return;
				}

				// 验证手机号格式
				if (!this.phoneNumber) {
					uni.showToast({
						title: '请输入手机号码',
						icon: 'none'
					});
					return;
				}

				if (!/^1[3-9]\d{9}$/.test(this.phoneNumber)) {
					uni.showToast({
						title: '请输入正确的手机号码',
						icon: 'none'
					});
					return;
				}

				// 开始倒计时
				this.startCountdown();

				// 这里可以调用获取验证码的API
				console.log('发送验证码到:', this.phoneNumber);
				uni.showToast({
					title: '验证码已发送',
					icon: 'success'
				});
			},

			// 开始倒计时
			startCountdown() {
				this.countdown = 60;
				this.timer = setInterval(() => {
					this.countdown--;
					if (this.countdown <= 0) {
						this.clearTimer();
					}
				}, 1000);
			},

			// 清除定时器
			clearTimer() {
				if (this.timer) {
					clearInterval(this.timer);
					this.timer = null;
				}
				this.countdown = 0;
			},

			// 提交表单
			submitForm() {
				if (!this.phoneNumber) {
					uni.showToast({
						title: '请输入手机号码',
						icon: 'none'
					});
					return;
				}

				if (!this.verificationCode) {
					uni.showToast({
						title: '请输入验证码',
						icon: 'none'
					});
					return;
				}

				// 这里可以调用提交的API
				console.log('提交数据:', {
					phone: this.phoneNumber,
					code: this.verificationCode
				});

				uni.showToast({
					title: '提交成功',
					icon: 'success'
				});
			}
		},

		// 页面卸载时清除定时器
		beforeDestroy() {
			this.clearTimer();
		},

		// 页面隐藏时清除定时器（刷新页面时会触发）
		onHide() {
			this.clearTimer();
		}
	}
</script>

<style>
	.container {
		background: linear-gradient(to bottom, #3284fc 50%, #f5f5f5 50%);
		height: 40vh;
		padding: 10rpx 20rpx;
		box-sizing: border-box;
	}

	.info {
		margin-top: 30rpx;
		background: #fff;
		border-radius: 30rpx;
		padding: 40rpx;
		box-sizing: border-box;
		box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.08);
	}

	/* 手机号输入框样式 */
	.tel {
		margin-bottom: 40rpx;
	}

	.input-group {
		display: flex;
		align-items: center;
		background: #f5f5f5;
		border-radius: 50rpx;
		padding: 20rpx 30rpx;
		position: relative;
	}

	.icon {
		width: 40rpx;
		height: 40rpx;
		margin-right: 20rpx;
		flex-shrink: 0;
	}

	.tel-input {
		flex: 1;
		font-size: 32rpx;
		color: #333;
		background: transparent;
		border: none;
		outline: none;
	}

	.clear-btn {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 40rpx;
		height: 40rpx;
		border-radius: 50%;
		position: absolute;
		right: 20rpx;
		top: 50%;
		transform: translateY(-50%);
		color: #5e5e5e;
	}

	.clear-icon {
		width: 40rpx;
		height: 40rpx;
	}


	/* 验证码区域样式 */
	.Yanzeng {
		display: flex;
		align-items: center;
		gap: 20rpx;
		margin-bottom: 60rpx;
	}

	.YanzengMa {
		flex: 1;
		display: flex;
		align-items: center;
		background: #f5f5f5;
		border-radius: 50rpx;
		padding: 20rpx 30rpx;
	}

	.code-input {
		flex: 1;
		font-size: 32rpx;
		color: #333;
		background: transparent;
		border: none;
		outline: none;
		margin-left: 20rpx;
	}

	.GetMa {
		background: #3284fc;
		border-radius: 50rpx;
		padding: 20rpx 30rpx;
		min-width: 150rpx;
		text-align: center;
	}

	.get-code-text {
		color: #fff;
		font-size: 28rpx;
		font-weight: 500;
	}

	/* 提交按钮样式 */
	.submit-btn {
		background: #0662e6;
		border-radius: 50rpx;
		padding: 15rpx 10rpx;
		width: 500rpx;
		margin: 0 auto;
		margin-top: 80rpx;
		text-align: center;
	}

	.submit-text {
		color: #fff;
		font-size: 36rpx;
		font-weight: 600;
	}

	/* placeholder样式 */
	.tel-input::placeholder,
	.code-input::placeholder {
		color: #999;
		font-size: 30rpx;
	}

	/* 按钮点击效果 */
	.GetMa:active,
	.submit-btn:active {
		opacity: 0.8;
		transform: scale(0.98);
	}
</style>